package com.wlstock.funi.MissionInstance;

import android.content.Context;

import com.em.mission.Mission;
import com.em.mission.MissionItem;
import com.em.mission.MissionListener;
import com.em.mission.MissionResultListener;
import com.wlstock.funi.R;
import com.wlstock.funi.app.FundApplication;
import com.wlstock.funi.request.NewStockCalendarRequest;
import com.wlstock.funi.runnable.OrderBuy;
import com.wlstock.funi.runnable.OrderListener;
import com.wlstock.funi.runnable.ZXMaxCount;
import com.wlstock.funi.utils.NotiIdGenerator;
import com.wlstock.funi.utils.NotiUtils;
import com.wlstock.funi.utils.SharePreferenceUtils;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.List;

/**
 * Created by admin on 16/8/2.
 *
 * 申购新股
 *
 * 1、判断今天申购中的新股
 * 2、获取对应额度
 * 3、申购
 * 4、返回申购结果:成功,将对应新股申购的标志位置为成功
 *
 *
 */

public class BuyXinMission extends MissionItem implements MissionListener, NewStockCalendarRequest.DataResponseListener, ZXMaxCount.RefreshListener, OrderListener {

    private int STATE_NOT_BUY = 1;
    private int STATE_HAS_BOUGHT = 2;


    private static BuyXinMission instance;

    private BuyXinMission(Context mContext,String name) {
        super(mContext,name);
    }

    public static BuyXinMission getInstance(Context mContext,String name) {
        if (instance == null) {
            synchronized (BuyXinMission.class) {
                if (instance == null) {
                    instance = new BuyXinMission(mContext, name);
                }
            }
        }
        return instance;
    }


    private NewStockCalendarRequest newStockCalendarRequest;

    @Override
    public void start(MissionResultListener resultListener){
        this.resultListener = resultListener;
        start(this, resultListener);
    }


    @Override
    public void beforeExecuteListener(Mission mission) {
        if (newStockCalendarRequest == null){
            newStockCalendarRequest = new NewStockCalendarRequest(this);
            newStockCalendarRequest.request();
        }

    }

    @Override
    public void onExecute(Mission mission) {

    }

    @Override
    public void afterExecute(Mission mission) {

    }


    @Override
    public void newStockList(List<Object> stockList) {
        if (stockList!=null){
            for (int i = 0; i < stockList.size(); i++){
                JSONObject stock = (JSONObject) stockList.get(i);
//                        "GpName": "安图生物",
//                        "SgKey": "732658",
//                        "Code": "603658",
//                        "FxCount": "1260",
//                        "SumFxCount": "4200",
//                        "FxPrice": "14.58",
//                        "FxPrice2": "14.58",
//                        "FxPrice3": "14.58",
//                        "XgCount": "12000.00",
//                        "XgCount2": "1.20",
//                        "Syl": "22.99",
//                        "Syl2": "22.99",
//                        "Syl3": "22.99",
//                        "Statu": "申购中",
//                        "SgDate": "2016-08-19"
                try {
                    String sgKey = stock.getString("SgKey");
                    String fxPrice = stock.getString("FxPrice");
                    if (shouldBuyXin(sgKey)){
                        ZXMaxCount maxCount = new ZXMaxCount(ZXMaxCount.BSFLAG_BUY,sgKey,fxPrice,stock);
                        maxCount.pListener = this;
                        maxCount.request();
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }

            }

        }
    }

    private boolean shouldBuyXin(String subCode) {
        int subCodeState = SharePreferenceUtils.getIntSP(FundApplication.getInstance(), "BUY_XIN"+subCode, STATE_NOT_BUY);
        if (subCodeState == STATE_NOT_BUY){
            return true;
        }else{
            return false;
        }
    }

    @Override
    public void onUpdateResponse(String stkCode,JSONObject stock ,int bsFlag, String response) {
        if (resultListener != null){
            try {
//                        "GpName": "安图生物",
//                        "SgKey": "732658",
//                        "Code": "603658",
//                        "FxCount": "1260",
//                        "SumFxCount": "4200",
//                        "FxPrice": "14.58",
//                        "FxPrice2": "14.58",
//                        "FxPrice3": "14.58",
//                        "XgCount": "12000.00",
//                        "XgCount2": "1.20",
//                        "Syl": "22.99",
//                        "Syl2": "22.99",
//                        "Syl3": "22.99",
//                        "Statu": "申购中",
//                        "SgDate": "2016-08-19"
                String gpName = stock.getString("GpName");
                String sgKey = stock.getString("SgKey");
                String code = stock.getString("Code");
                String fxPrice = stock.getString("FxPrice");
                String statu = stock.getString("Statu");

                int maxCount = parseMaxCout(response);
                if (maxCount != -1){
                    SharePreferenceUtils.setIntSP(FundApplication.getInstance(),"BUY_XIN"+stkCode,STATE_HAS_BOUGHT);
                    if (maxCount!=0){
                        try {
                            OrderBuy buy = new OrderBuy(stkCode,gpName, Float.valueOf(fxPrice), Long.valueOf(maxCount));
                            buy.buy(this);
                        }catch (Exception e){
                            e.printStackTrace();
                        }
                    }
                }
            } catch (JSONException e) {
                e.printStackTrace();
            }
        }
    }

    private int parseMaxCout(String response) {
        try {
            return Integer.valueOf(response);
        }catch (Exception e){
            return -1;
        }
    }

    @Override
    public void orderSuccess(int typeType, String stkcode, String stkName, float price, long qty) {
        NotiUtils.showSimpleNoti(FundApplication.getInstance(), NotiIdGenerator.ID_NEW_STOCK_SUCCESS,"新股:"+stkName+"申购成功","申购份额:"+qty, R.drawable.ic_operator);
        if (resultListener!=null){
            resultListener.executeSuccess("OK",this);
        }
    }

    @Override
    public void orderFaile(int tradeType, String stkcode, String stkName, float price, long qty, String info) {
        if (info!=null){
            NotiUtils.showSimpleNoti(FundApplication.getInstance(), NotiIdGenerator.ID_NEW_STOCK_SUCCESS,"新股:"+stkName+"申购失败","失败原因:"+info, R.drawable.ic_operator);
        }
    }
}
